<?php
   namespace Home\Controller;
   // use Think\Controller;
    class GoodsController extends EmptyController{ 
        public function index(){
             //顶部判断，用户是否登录
            $username = session('user');
            
            $this->assign('username',$username['username']);
            //------------------------------
            $goods = D('goods');
            $id = I('get.id');
            $list = $goods->find($id); 
           // dump($list);
            //找到名
            $tname = $list['typeid'];
            // dump($tid);
            $type = M('type');
            //查询商品类别表
            $tcom = $type->select();
            //找到类别id
            $map['name'] = ['eq',$tname]; 
            $tlist = $type->where($map)->find();
            $tid = $tlist['id'];
            // dump($tid);
            //查找路径(面包屑);
            function breadcrumbs(&$arr, $id)
            {
                foreach ($arr as $row)
                {
                    if ($row['id'] == $id)
                    {
                        $tmp[] = $row['name'];
                        if (!empty($row['pid']))
                        {
                            $tmp = array_merge(breadcrumbs($arr, $row['pid']), $tmp);
                        }
                    }
                }
                if (is_array($tmp) && !empty($tmp))
                {
                    return $tmp;
                }
                else
                {
                    return array();
                }
            }
             
            $arr = breadcrumbs($tcom, $tid);
            $str = implode(' > ', $arr);
            //分配面包屑数据
            $this->assign('com',$str);
            //分配商品信息数据
            $list['addtime'] = date('Y-m-d H:i:s',$list['addtime']);
            $this->assign('list',$list);
           //查询商品数据
            $norm = M('norms');
            $gid['gid'] = ['eq',$id];
            $norm = $norm->where($gid)->select();
            //分配商品数据
            $this->assign('norm',$norm);
            //分配型号
            $size = $norm[0]['size'];
            //分配颜色
            $color = $norm[0]['color'];
            $size = explode(',',$size);
            $color = explode(',',$color);
            $this->assign('size',$size);
            $this->assign('color',$color);
            //商品参数
            //查询商品参数表
            $param = M('param');
            $gid['gid'] = ['eq',$id];
            $param = $param->where($gid)->select();
            $this->assign('param',$param[0]);
            //猜你喜欢
            $goods = D('goods');
            $like  = $goods->goods_like();
            // dump($like);
            $this->assign('like',$like);
            //查询详情大图
            $img = M('img');
            //设置查询条件
            $gid = [];
            $gid['gid'] = ['eq',$id];
            $img = $img->where($gid)->select();
            // dump($img);
            $this->assign('img',$img);
            //调用分类列表
            $this->type_secl();
            //调用友情链接
            $this->link_s();
            //调用导航栏
            $this->bar();
           //==2017_1_25  调用评论方法==========================================================================
            $this->show_comment();
            $this->display();
          

    }
     //首页分类查询方法
    public function type_secl(){
       $type = D('type');
       $t_list = $type->type_sec();
       $this->assign('t_list',$t_list);
    }
    //首页友链查询方法
    public function link_s(){
        $links = D('links');
        $link = $links->links_sec();
        // dump($list);
        $this->assign('link',$link);
    }
    //输出导航栏
    public function bar(){
        $type = D('type');
        $t_bar = $type->bar_sec();
        $this->assign('bar',$t_bar);
    }
    //==================2017_1_25 展示评论方法====
    public function show_comment(){
        $gid = I('get.id');
        //设置查询条件
        $map['gid'] = ['eq',$gid];
        //实例化评论表comment
        $comment = M('comment');
        //实例化用户表
        $user = M('user');
        //实例化用户详情表
        $userinfo = M('user_info');
        //执行查询
        $clist = $comment->where($map)->select();
        $num = count($clist);
        for ($i=0; $i < $num; $i++) { 
            $map = [];
            $map['id'] = ['eq',$clist[$i]['uid']];
            $ulist = $user->where($map)->find();
            $clist[$i]['username'] = $ulist['username'];
            $map = [];
            $map['uid'] = ['eq',$clist[$i]['uid']];
            $info = $userinfo->where($map)->find();
            if($info['picname']){
               $clist[$i]['userpic'] = $info['picname']; 
           }else{
                $clist[$i]['userpic'] = "dabai.jpg"; 
           }
            
        }
        // dump($ulist);
        $this->assign('clist',$clist);
        // dump($clist);
    }
    //=============结束==
}